package com.yj.event_listener.callback;

import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

import com.yj.event_listener.constants.Constants;
import com.yj.event_listener.interfaces.NotificationCallback;
import com.yj.event_listener.util.LogUtil;
import com.yj.event_listener.util.RemarkUtil;

public class CallStateListener extends PhoneStateListener {

    private final Context context;
    private final NotificationCallback notificationCallback;

    public CallStateListener(Context context, NotificationCallback callback) {
        this.context = context;
        this.notificationCallback = callback;
    }


    @Override
    public void onCallStateChanged(int state, String phoneNumber) {
        super.onCallStateChanged(state, phoneNumber);
        switch(state){
            /** Device call state: No activity.  */
            case TelephonyManager.CALL_STATE_IDLE:
                if(Constants.ringing){
                    Constants.ringing = false;
                    if(notificationCallback != null){
                        notificationCallback.onHangUp();
                    }
                }
                if(!Constants.hangup){
                    Constants.hangup = true;
                }
                LogUtil.d("TelephonyManager.CALL_STATE_IDLE " + phoneNumber + " ringing "+Constants.ringing + " hangup " + Constants.hangup);
                break;
            /** Device call state: Off-hook. At least one call exists
             * that is dialing, active, or on hold, and no calls are ringing
             * or waiting. */
            case TelephonyManager.CALL_STATE_OFFHOOK:
                if(Constants.ringing){
                    Constants.ringing = false;
                    if(notificationCallback != null){
                        notificationCallback.onHangUp();
                    }
                }
                LogUtil.d("TelephonyManager.CALL_STATE_OFFHOOK " + phoneNumber + " ringing "+Constants.ringing + " hangup " + Constants.hangup);
                break;
            /** Device call state: Ringing. A new call arrived and is
             * ringing or waiting. In the latter case, another call is
             * already active.  */
            case TelephonyManager.CALL_STATE_RINGING:
                if(!Constants.ringing){
                    Constants.ringing = true;
                }
                if(Constants.hangup){
                    Constants.hangup = false;
                }
                LogUtil.d("TelephonyManager.CALL_STATE_RINGING " + phoneNumber + " ringing " + Constants.ringing + " hangup " + Constants.hangup);
                if(phoneNumber != null && !phoneNumber.isEmpty()){

//                    // Retrieve contact ID from the phone number
//                    String contactId = RemarkUtil.getContactIdFromPhoneNumber(context, phoneNumber);
//                    LogUtil.d("contactId " + contactId);
//
//                    // Retrieve contact remark using contact ID
//                    String remark = RemarkUtil.getContactRemarkFromId(context, contactId);
//                    LogUtil.d("remark " + remark);

                    String title = RemarkUtil.getSenderTitle(context, phoneNumber);
                    LogUtil.d("title "+ title);

                    if(notificationCallback != null){
                        notificationCallback.onIncomingCall("", title == null || title.isEmpty() ? phoneNumber : title);
                    }

                }
                break;
        }




    }
}
